<template>
  <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="菜品名称" prop="cname">
      <el-input v-model="ruleForm.cname"></el-input>
    </el-form-item>
    <el-form-item label="菜品描述" prop="cdesc">
      <el-input v-model="ruleForm.cdesc"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import axios from "axios";
    export default {
        name: "CategoryUpdate",
      data() {
        return {
          ruleForm: {
            cname: '',
            cdesc: '',
          },
        };
      },
      methods: {
        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              axios.post("/api/updateCategory",this.ruleForm).then(res=>{
                if (res.data=="success"){
                  this.$router.push("/categoryList")
                }else{
                  alert("修改失败")
                }
              })
            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
        },
      },
      mounted(){
        var cid =  this.$route.params.id
        //通过id 去后台进行查询数据
        // alert(fid)

        axios.get("/api/getByCid/"+cid).then(res=>{
          this.ruleForm=res.data;
        })


      },

    }
</script>

<style scoped>

</style>
